//index.js
//获取应用实例
var app = getApp()
// 在页面中定义激励视频广告对象
let rewardedVideoAd = null;

var address = '';
//var message = '观看15-30秒激励视频广告，即可前往查看，是否观看';

//var fs = wx.getFileSystemManager();

Page({
  data: {
        url:app.globalData.userInfo == null?'/i/bgimg.jpg':''+app.globalData.userInfo.avatarUrl
  },
 
  onShow(){
      //读文件测试
      /** console.info(' OnShow>>>>>>>app.globalData.userInfo========'+app.globalData.userInfo)

      if(!(app.globalData.userInfo===null)){
        console.info('imgUrl==========='+app.globalData.userInfo.avatarUrl)
      } **/
      
      /** 
      fs.copyFile({
          srcPath : "test.txt",
          destPath : wx.env.USER_DATA_PATH + "/test.txt",
          success : res =>{
                console.info(res)
          },
          fail : console.error
      })
      **/

      

      // fs.readFile({
      //       filePath: wx.env.USER_DATA_PATH + "/i/bgimg.jpg",
      //       encoding:"utf-8",
      //       success: res =>{
      //             console.log(res)
      //       },fail:console.error
      // })
      
  },

  test(e){
       var that = this;
       console.log('==============test')
       console.log(e.detail)
       //登录成功后set全局对象
       app.globalData.userInfo = e.detail.userInfo;
        console.info('app.globalData.userInfo========'+app.globalData.userInfo)
       //that.onShow()    avatarUrl
    
       that.setData({
             url:''+app.globalData.userInfo.avatarUrl
       })
  },

  test2(e){
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  },

  testVideo(){
        wx.navigateTo({
          url: '/pages/video/video',
        })      
  },


  btnfun1(){
    address = 'http://www.ahqao.com';
    return this.showVideo();
  },

  btnfun2(){
      address = 'http://www.ahqao.com/jt';
      return this.showVideo();
  },



  /**
   * imageLoad: function(e) {
        var $width=e.detail.width,    //获取图片真实宽度
            $height=e.detail.height,
            ratio=$width/$height;    //图片的真实宽高比例
        var viewWidth=1500,           //设置图片显示宽度，左右留有16rpx边距
            viewHeight=718/ratio;    //计算的高度值
        var image=this.data.images; 
        //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
        image[e.target.dataset.index]={
            width:viewWidth,
            height:viewHeight
        }
        this.setData({
              images:image
        })
 },  **/

  showVideo(){
    rewardedVideoAd.show()
    .catch(() => {
        rewardedVideoAd.load()
        .then(() => rewardedVideoAd.show())
        .catch(err => {
          console.log('激励视频 广告显示失败')
        })
    })
 },
  
  onLoad() {

    if (wx.createRewardedVideoAd) {
      rewardedVideoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-2017893ecea5db2f'
      })
      rewardedVideoAd.onLoad(() => {
        console.log('onLoad event emit')
      })

      //捕捉错误
      rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
       // 监听关闭
      rewardedVideoAd.onClose((res) => {
        console.log('onClose event emit', res)
        // 用户点击了【关闭广告】按钮
        if (res && res.isEnded) {
          // 正常播放结束，可以下发游戏奖励
          console.log('正常播放结束，可以下发奖励')

          //开始下发奖励
          wx.showModal({
            title: '温馨提示',
            content: '下载链接复制成功，黏贴到浏览器打开即可',
            success (res) {

               //下发奖励将地址复制到剪切板
               wx.setClipboardData({
                data: address,
                success: function(res) {
                  wx.getClipboardData({
                    success: function(res) {
                      console.log(res.data) // data
                    }
                  })
                }
              })

              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

        } else {
          // 播放中途退出，不下发游戏奖励
          console.log('播放中途退出，不下发奖励')

          wx.showModal({
            title: '温馨提示',
            content: '操作失败，原因:广告视频未完成观看',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

        }
  })

     
    }
  }


})
